home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / v3_1 / sbp3_1e.lzh / TOPLEVEL.PL < prev    next >
Text File  |  1991-10-31  |  2KB  |  57 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5.  
  6. /* TOPLEVEL.PL */
  7. /* A customized user interface for Prolog */
  8.  
  9. /********************************************
  10.  * top_level                                *
  11.  *   Endless repeat-fail loop.              *
  12.  *   Get a query and display its solutions. *
  13.  ********************************************/
  14.  
  15. top_level :-   repeat,
  16.                nl,
  17.                write('Type a query: '),
  18.                read(Q),
  19.                find_solutions(Q),
  20.                fail.
  21.  
  22. /**************************************************
  23.  * find_solutions(Q)                              *
  24.  *   Call Q, then display Q with instantiations,  *
  25.  *   then ask if more solutions are wanted.       *
  26.  *   If so, backtrack; if not, cut.               *
  27.  *   If we run out of solutions and the cut has   *
  28.  *   not been executed, go to the second clause,  *
  29.  *   which displays a message.                    *
  30.  **************************************************/
  31.  
  32. find_solutions(Q) :-  call(Q),
  33.                       write('Solution found:    '),
  34.                       write(Q),
  35.                       nl,
  36.                       write('Look for another? (Y/N):'),
  37.                       get(Char), nl,
  38.                       (Char = 78 ; Char = 110),   /* N or n */
  39.                       !.
  40.  
  41. find_solutions(_) :-  write('No (more) solutions'),
  42.                       nl.
  43.  
  44.  
  45. /* Sample knowledge base */
  46.  
  47. father(michael,cathy).
  48. mother(melody,cathy).
  49. parent(X,Y) :- father(X,Y).
  50. parent(X,Y) :- mother(X,Y).
  51.  
  52.  
  53. /* Starting query */
  54.  
  55. start :- top_level.
  56. :-start.
  57.